from django.shortcuts import render
from django.http import HttpResponse
from django.views import View
from django import forms
from app1.forms import UserInfoForm
# Create your views here.

class UserInfo(View):
    #username = forms.CharField()
    #password = forms.CharField()
    #mobile = forms.CharField()
    #email = forms.EmailField()

    def get(self, request):

        userInfoForm = UserInfoForm();
        print(userInfoForm)
        return render(request, 'app1/UserInfo.html', {
            'userInfoFomr': userInfoForm
        })

    def post(self, request):
        user_input_obj = UserInfoForm(request.POST)
        if user_input_obj.is_valid(): 
            print('{:-^90}'.format('淫荡的分割线'))
            print(user_input_obj)
            print('{:-^90}'.format('淫荡的类型'))
            print(type(user_input_obj))
            print('{:-^90}'.format('淫荡的属性'))
            print(user_input_obj.cleaned_data)
            #print(user_input_obj.username)
            #print(user_input_obj.password)
            #print(user_input_obj.mobile)
            #print(user_input_obj.email)
            print(user_input_obj.cleaned_data['username'])
            print(user_input_obj.cleaned_data['password'])
            print(user_input_obj.cleaned_data['mobile'])
            print(user_input_obj.cleaned_data['email'])
            print(user_input_obj.cleaned_data['usertype'])
            print('{:-^90}'.format('淫荡的分割线'))
        else:
            error_msg = user_input_obj.errors
            print('{:-^90}'.format('错误的分割线'))
            print(error_msg)
            print('{:-^90}'.format('错误的分割线'))
            print(user_input_obj)
            print('{:-^90}'.format('错误的分割线'))
            return render(request, 'app1/UserInfo.html', {
                'userInfoFomr': user_input_obj,
                'errors': error_msg,
                'errorjson': error_msg.as_json()
            })
        return HttpResponse('OK')

def index(request):
    
    #user_input_obj = UserInfo(request.POST)
    #print(user_input_obj.username)
    #print(user_input_obj.password)
    #print(user_input_obj.mobile)
    #print(user_input_obj.email)
    return HttpResponse('APP1  OK');
